package com.example.healkep.MainActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
import com.example.healkep.R;
import com.example.healkep.echarts.EchartOptionUtil;
import com.example.healkep.echarts.EchartView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivityShowSleepDay extends Activity {
    private int[] day_sleep;
    private EchartView barChart,pieChart;
    private TextView textView1, textView2;
    private int sleep_sum = 0;  //记录cal总数
    private int sleep_ave = 0; // 配合sum计算总平均值
    private String fin_sleep_sum = "0";  //记录cal总数
    private String fin_sleep_ave = "0"; // 配合sum计算总平均值
    private int sleep_count = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate ( savedInstanceState );
        setContentView ( R.layout.activity_show_sleep_day );
        Intent intent = getIntent ();
        Bundle bundle = intent.getExtras ();
        day_sleep = bundle.getIntArray ( "day_sleep" );
        System.out.println("cal: ");
        System.out.println(day_sleep[0]);
        System.out.println(day_sleep[1]);
        System.out.println(day_sleep[2]);
        for(int i=0; i<3; i++){
            sleep_sum += day_sleep[i];
            if(day_sleep[i] != 0) {
                sleep_count++;
            }
        }
        if(sleep_count != 0){
            sleep_ave = sleep_sum / sleep_count;
        }
        fin_sleep_ave = sleep_ave + "分钟";
        fin_sleep_sum = sleep_sum + "分钟";
        textView1 = findViewById(R.id.sleep_sum);
        textView2 = findViewById(R.id.sleep_ave);
        textView1.setText(fin_sleep_sum);
        textView2.setText(fin_sleep_ave);
        barChart = findViewById(R.id.BarChart);
        barChart.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                //最好在h5页面加载完毕后再加载数据，防止html的标签还未加载完成，不能正常显示
                refreshBarChart();
            }
        });
        pieChart = findViewById(R.id.PieChart);
        pieChart.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                //最好在h5页面加载完毕后再加载数据，防止html的标签还未加载完成，不能正常显示
                refreshPieChart();
            }
        });
    }

    private void refreshBarChart(){
        Object[] x = new Object[]{
                "深度睡眠", "中度睡眠", "浅层睡眠"
        };
        Object[] y = new Object[]{
                day_sleep[0], day_sleep[1], day_sleep[2]
        };
        barChart.refreshEchartsWithOption(EchartOptionUtil.getBarChartOptions(x, y));
    }
    private void refreshPieChart(){

        String deep = "深度睡眠: " + Integer.toString(day_sleep[0])+ " 分钟";
        String middle = "中度睡眠: " + Integer.toString(day_sleep[1])+ " 分钟";
        String shallow = "浅层睡眠: " + Integer.toString(day_sleep[2])+ " 分钟";
        List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
        HashMap hashMap1 = new HashMap();
        hashMap1.put("value", day_sleep[0]);
        hashMap1.put("name", deep);
        HashMap hashMap2 = new HashMap();
        hashMap2.put("value", day_sleep[1]);
        hashMap2.put("name", middle);
        HashMap hashMap3 = new HashMap();
        hashMap3.put("value", day_sleep[2]);
        hashMap3.put("name", shallow);
        //pie.data(hashMap,hashMap1,hashMap2);

        data.add(hashMap1);
        data.add(hashMap2);
        data.add(hashMap3);

        pieChart.refreshEchartsWithOption(EchartOptionUtil.getPieChartOptions(data));
    }
}
